home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Devices / Apple Desktop Bus / ADB Key Spy / Sources / ADB Key Spy.h < prev    next >
Encoding:
Text File  |  1995-09-20  |  4.3 KB  |  136 lines  |  [TEXT/MMCC]

  1.     //
  2.     //    ADB Key Spy • Pete Gontier • gurgle@apple.com
  3.     //    Macintosh Developer Technical Support
  4.     //    © 1995 Apple Computer, Inc.
  5.     //
  6.     //    This module provides something of a replacement for GetKeys by
  7.     //    maintaining a key map reflecting the state of each keyboard
  8.     //    attached to the Mac via ADB. (This means it won't work with any
  9.     //    Mac earlier than an SE.)
  10.     //
  11.     //    The module is intended to solve a problem a game developer
  12.     //    was having with the Adjustable Keyboard, which appears on ADB as
  13.     //    two keyboards. The ROM keyboard driver has one map and clears it
  14.     //    whenever there is a key state change on a new keyboard.
  15.     //    Consequently, it was impossible for said developer to allow
  16.     //    users to steer on the numeric key pad and fire with the space bar.
  17.     //
  18.     //    Although this module does maintain a map for each keyboard, the
  19.     //    module does not provide an API for polling individual keyboards.
  20.     //    You can only find out if a key with a given key code is being
  21.     //    held down on any keyboard. If you need this functionality, it
  22.     //    should be pretty simple to add. (That's why you get the source...)
  23.     //
  24.     //    Finally, this package has the limitation that if you call
  25.     //    AKS_AcquireKeyboards while a key is being held down,
  26.     //    AKS_IsKeyDown will not return true for that key until the key
  27.     //    is released and held down again. This is the peril of not
  28.     //    being the ROM keyboard driver.
  29.     //
  30.  
  31. #pragma once
  32.  
  33. #ifndef __DESKBUS__
  34. #    include <DeskBus.h>
  35. #endif
  36.  
  37.     //
  38.     //    kMaxKeyCode
  39.     //
  40.     //    GetKeys returns four 32-bit quantities for its key map, and with
  41.     //    each bit corresponding to a key, that 32*4==128 keys. I don't know
  42.     //    if keyboards are capable of supporting more keys, but it's unlikely
  43.     //    anybody cares about key codes that high simply because GetKeys
  44.     //    can't report them. We use 127 because key codes start at 0.
  45.     //
  46.  
  47. static const unsigned char kMaxKeyCode = 127;
  48.  
  49.     //
  50.     //    tKeyboardInfo
  51.     //
  52.     //    This structure describes each keyboard discovered during AKS_AcquireKeyboards.
  53.     //    In it we store info from the service routine for the keyboard before we replace it,
  54.     //    plus a key map. We use a byte for each entry in the key map mostly for historical
  55.     //    reasons. (There used to be one key map for all keyboards; each entry was a counter.)
  56.     //    Now it has a byte per entry simply because I am too lazy to write the bit-twiddling
  57.     //    code to make it one bit per entry.
  58.     //
  59.     //    We maintain a simple linked list of these structures. Client code doesn't need to
  60.     //    worry about this structure; it's just here for the benefit of the code resource.
  61.     //
  62.  
  63. #if PRAGMA_ALIGN_SUPPORTED
  64. #    pragma options align=mac68k
  65. #endif
  66.  
  67. typedef struct
  68. {
  69.     unsigned char                raw;
  70.     Boolean                        noXor, Xor;
  71.     unsigned char                bits;
  72.     unsigned char                pstring [ ];
  73. }
  74. tKeyMapResourceException;
  75.  
  76. typedef struct // for corroboration, see "SysTypes.r"
  77. {
  78.     unsigned short                id;
  79.     unsigned short                vers;
  80.     unsigned char                map [kMaxKeyCode + 1];
  81.     unsigned short                exceptionCount;
  82.     tKeyMapResourceException    exceptions [ ];
  83. }
  84. tKeyMapResource, *tKeyMapResourceP, **tKeyMapResourceH;
  85.  
  86. typedef struct tKeyboardInfo
  87. {
  88.     ADBServiceRoutineUPP    dbServiceRtPtr;
  89.     Ptr                        dbDataAreaAddr;
  90.     unsigned char            virtualKeyMap [kMaxKeyCode + 1];
  91.     tKeyMapResourceH        keyMapResH;
  92.     struct tKeyboardInfo    *next;
  93. }
  94. tKeyboardInfo;
  95.  
  96. #if PRAGMA_ALIGN_SUPPORTED
  97. #    pragma options align=reset
  98. #endif
  99.  
  100.     //
  101.     //    AKS_AcquireKeyboards
  102.     //
  103.     //    Walks the ADB device list and patches the service routine
  104.     //    of each keyboard so that an extra (internal) key map is maintained.
  105.     //    A block of memory is allocated via NewPtr for each keyboard.
  106.     //
  107.     //    You will probably want to call this function when your app
  108.     //    starts and on resume events.
  109.     //
  110.  
  111. pascal OSErr AKS_AcquireKeyboards (void);
  112.  
  113.     //
  114.     //    AKS_RelinquishKeyboards
  115.     //
  116.     //    Walks the private internal list of patched ADB keyboard
  117.     //    service routines and unpatches them.
  118.     //
  119.     //    You will probably want to call this function when your app
  120.     //    quits and on suspend events. If you patch ExitToShell for
  121.     //    cleanup during emergency exits, you will want to call this
  122.     //    in your patch.
  123.     //
  124.  
  125. pascal OSErr AKS_RelinquishKeyboards (void);
  126.  
  127.     //
  128.     //    AKS_IsKeyDownAnywhere
  129.     //
  130.     //    Tests all key maps to see whether the key corresponding to
  131.     //    the given key code is down. Pass a value from 0 to 127; higher
  132.     //    values always produce false.
  133.     //
  134.  
  135. pascal Boolean AKS_IsKeyDownAnywhere (unsigned char keyCode);
  136.